const { Fruit, Op } = require('../models/fruit');

const Index = async ctx => {

  let { page=1, perpage=2, keyword='' } = ctx.query;

  let { count, rows } = await Fruit.findAndCountAll({
    where: {
      [Op.or]: [
        {
          name: {
            [Op.like]: '%'+keyword+'%'
          }
        },
        {
          price: {
            [Op.like]: '%'+keyword+'%'
          }
        }
      ]
    },
    order:[['id','desc']],
    offset: (page - 1) * perpage,
    limit:  +perpage,
    raw: true
  });

  ctx.render('index.html', {
    title: '水果管理',
    page: parseInt(page),
    pagecount: Math.ceil(count/perpage),
    perpage,
    keyword,
    fruit: rows
  });

}

module.exports = {
  'GET /': Index
}